package Messages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.Phaser;

import com.mysql.jdbc.ResultSet;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;
/**
 * This class implements the AddLock to db
 *   @author Shlomit Yosfan
 * @author Nofar Sutliman
 */
public class MeassageAddLock {

	private ConnectionToClient client ;
	private String[] str; 
	static int row1;
	
	/**
	 * Contractor of this class 
	 * @param str
	 * @param client
	 */
	public MeassageAddLock(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 
	}


	/**
	 * Adding  Lock to the DB	
	 * @throws SQLException
	 * @throws IOException
	 */
	
	public void AddLock() throws SQLException, IOException{
		
		SQLConnection sc = new SQLConnection();
		
		java.sql.PreparedStatement lockId = sc.getConnection().prepareStatement("SELECT * FROM db.lock;");

		java.sql.PreparedStatement lockInsert = sc.getConnection().prepareStatement("INSERT INTO db.lock (LockID, LockType,MinInventory,MaxInventory,SuppID,Quantity) VALUES(?,?,?,?,?,?);");


		
		
		ResultSet rs= (ResultSet) lockId.executeQuery();
		row1 = 0;
		while(rs.next()){
			int temp = rs.getInt(1);
				if(row1>temp)
					row1=row1;
					else{
					row1=temp;}

		}
		
	
		lockInsert.setInt(1, row1+1);					
		lockInsert.setString(2,str[1]);
		lockInsert.setString(3,str[2]);
		lockInsert.setString(4,str[3]);
		lockInsert.setString(5, str[4]);
		lockInsert.setString(6, str[5]);

		lockInsert.executeUpdate();
		lockInsert.close();
	
		
							ArrayList<Object> lock=new ArrayList<Object>();
							lock.add("AddLock");		
							client.sendToClient(lock);
		
		
		
		
	}
				
}
